﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using RooHui.Infrastructure.Entities.EFContext;

namespace RooHui.Infrastructure.Entities.Repository
{
    [Table("P_Member")]
    public class P_Member : IEntity<P_Member>
    {
        [Key]
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string UserName { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string Password { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(64)]
        public string RealName { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(50)]
        public string Photo { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(11)]
        public string HandPhone { get; set; }
        [Column(TypeName = "bit")]
        public bool HandPhoneCheck { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(20)]
        public string IDCardNo { get; set; }
        [Column(TypeName = "bit")]
        public bool IDCardCheck { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string Email { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(100)]
        public string Token { get; set; }
        [Column(TypeName = "bit")]
        public bool EmailCheck { get; set; }
        public int MemberType { get; set; }
        [Column(TypeName = "bit")]
        public bool IsActive { get; set; }
        public DateTime CreateTime { get; set; }
        public int CreateType { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(20)]
        public string CreateUser { get; set; }
        public int Gender { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(20)]
        public string Education { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(10)]
        public string MaritalStatus { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(100)]
        public string Address { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(20)]
        public string RegisterIP { get; set; }
        public int Status { get; set; }
        [Column(TypeName="varchar")]
        [StringLength(128)]
        public string Agent { get; set; }

        public override P_Member Find(string key)
        {
            return Find(c => c.UserName == key);
        }

        public override P_Member Find(System.Linq.Expressions.Expression<Func<P_Member, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Member.FirstOrDefault(where);
                if (item != null)
                {
                    this.Address = item.Address;
                    this.CreateTime = item.CreateTime;
                    this.CreateType = item.CreateType;
                    this.CreateUser = item.CreateUser;
                    this.Education = item.Education;
                    this.Email = item.Email;
                    this.EmailCheck = item.EmailCheck;
                    this.Gender = item.Gender;
                    this.HandPhone = item.HandPhone;
                    this.HandPhoneCheck = item.HandPhoneCheck;
                    this.IDCardCheck = item.IDCardCheck;
                    this.IDCardNo = item.IDCardNo;
                    this.IsActive = item.IsActive;
                    this.Status = item.Status;
                    this.MaritalStatus = item.MaritalStatus;
                    this.MemberType = item.MemberType;
                    this.Password = item.Password;
                    this.Photo = item.Photo;
                    this.RealName = item.RealName;
                    this.RegisterIP = item.RegisterIP;
                    this.Token = item.Token;
                    this.UserName = item.UserName;
                    this.Agent = item.Agent;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<P_Member, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Member.Any(where);
            }
        }
    }
}